package com.brand.prisd.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.brand.prisd.R;
import com.orhanobut.logger.Logger;
import com.tuya.smart.android.user.api.IValidateCallback;
import com.tuya.smart.home.sdk.TuyaHomeSdk;

/**
 * 验证码登录界面
 */
public class VerificationCodeLoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_phoneNum;
    private Button btn_getSecurityCode;
    private String phoneNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verification_code_login);
        initView();

    }

    private void initView() {
        et_phoneNum = findViewById(R.id.et_phoneNum);
        btn_getSecurityCode = findViewById(R.id.btn_getSecurityCode);
        btn_getSecurityCode.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_getSecurityCode:
                getSecurityCode();
                InputCode();
                break;
        }
    }

    private void InputCode() {
        Intent intent = new Intent(this, InputCodeActivity.class);
        intent.putExtra("phoneNum", phoneNum);
        startActivity(intent);
    }

    /**
     * 获取验证码
     */
    private void getSecurityCode() {
        phoneNum = et_phoneNum.getText().toString();
        Logger.d("phoneNum: " + phoneNum);
        //获取手机验证码
        TuyaHomeSdk.getUserInstance().getValidateCode("86", phoneNum, new IValidateCallback() {
            @Override
            public void onSuccess() {
                Toast.makeText(getApplicationContext(), "获取验证码成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(String code, String error) {
                Toast.makeText(getApplicationContext(), "code: " + code + "error:" + error, Toast.LENGTH_SHORT).show();
            }
        });

    }
}
